/**
 * 线
 * */

export default {
    data() {
        return {
        }
    },
    created() {
        this.handlersPolygon = null
    },
    methods: {

        onToolPolygon() {
            var isNew = true
            var positions = []
            var hierarchy
            var entityLine, drawLayer, dataSourceName
            var addInfoPoint = cartesian => {
                const _labelEntity = {
                    position: cartesian,
                    eventName: 'DrawTool',
                    dataSourceName: dataSourceName
                }

                _labelEntity.point = {
                    pixelSize: 2,
                    outlineColor: Cesium.Color.BLUE,
                    outlineWidth: 2,
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
                }
                drawLayer.entities.add(_labelEntity)
            }
            this.handlersPolygon = new Cesium.ScreenSpaceEventHandler(this.$viewer.scene.canvas)
            // left
            this.handlersPolygon.setInputAction(movement => {
                var cartesian = this.getCatesian3FromPX(movement.position)
                if (cartesian && cartesian.x) {
                    if (isNew) {
                        isNew = false
                        positions = []
                        hierarchy = new Cesium.PolygonHierarchy()
                        dataSourceName = 'DrawToolPolygon' + new Date().getTime()
                        drawLayer = new Cesium.CustomDataSource(dataSourceName)
                        entityLine = drawLayer.entities.add({
                            eventName: 'DrawTool',
                            dataSourceName: dataSourceName,
                            polyline: {
                                positions: new Cesium.CallbackProperty((time, result) => {
                                    return positions
                                }, false),
                                width: 2,
                                material: Cesium.Color.WHITE.withAlpha(0.8),
                                clampToGround: true
                            },
                            polygon: {
                                hierarchy: new Cesium.CallbackProperty((time, result) => {
                                    return hierarchy
                                }, false),

                                material: Cesium.Color.WHITE.withAlpha(0.2),
                                classificationType: Cesium.ClassificationType.BOTH
                            }
                        })
                        this.$viewer.dataSources.add(drawLayer)
                        positions.push(cartesian.clone())
                        hierarchy.positions.push(cartesian.clone())
                    }
                    addInfoPoint(cartesian, dataSourceName)
                    positions.push(cartesian.clone())
                    hierarchy.positions.push(cartesian.clone())
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

            this.handlersPolygon.setInputAction(movement => {
                var cartesian = this.getCatesian3FromPX(movement.endPosition)
                if (positions.length >= 2) {
                    if (cartesian && cartesian.x) {
                        positions.pop()
                        positions.push(cartesian)
                        hierarchy.positions.pop()
                        hierarchy.positions.push(cartesian)
                    }
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // right
            this.handlersPolygon.setInputAction(movement => {
                if (positions.length === 0) return
                if (positions.length < 3) {
                    this.$viewer.dataSources.remove(drawLayer, true)
                } else {
                    positions.pop()
                    positions.push(positions[0])
                    hierarchy.positions.pop()
                    entityLine.polyline.positions = positions // 解决不能删除单根线的问题
                    entityLine.polygon.hierarchy = hierarchy
                }
                this.offToolPolygon()
                this.onToolPolygon()
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
        },
        offToolPolygon() {
            if (this.handlersPolygon) {
                this.handlersPolygon.destroy()
                this.handlersPolygon = null
            }
        }
    }
}
